﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CreateFurProject
{
    class Program
    {
        static string oldvalue = "Fur";
        static string newvalue = "";
        static void Main(string[] args)
        {
            Console.WriteLine($"提示：把控制台exe程序和Fur文件夹放到一起，Fur文件夹放入需要替换的内容，然后运行exe，输入新名字，回车，替换完成");
            var dictorypath = System.IO.Directory.GetCurrentDirectory() + "\\Fur";
            Console.WriteLine($"当前目录：{dictorypath}");
            Console.WriteLine($"请输入替换为新的名称：---");
            newvalue = Console.ReadLine();

            Console.WriteLine($"把Fur 替换为：{newvalue}");
            Console.WriteLine($"开始替换");

            DirectoryInfo theFolder = new DirectoryInfo(dictorypath);
            DirectoryInfo[] dirInfo = theFolder.GetDirectories();
            //遍历文件夹
            foreach (DirectoryInfo NextFolder in dirInfo)
            {

                // this.listBox1.Items.Add(NextFolder.Name);
                FileInfo[] fileInfo = NextFolder.GetFiles();
                foreach (FileInfo NextFile in fileInfo)  //遍历文件
                {
                    // this.listBox2.Items.Add(NextFile.Name);
                    ReplaceValue(NextFile.FullName, oldvalue, newvalue);
                    //替换新名称
                    if (NextFile.Name.Contains(oldvalue))
                    {
                        var a = Regex.Replace(NextFile.Name, oldvalue, newvalue);
                        var b = NextFile.DirectoryName + "\\" + a;
                        File.Move(NextFile.FullName, b);
                    }
                }

                if (NextFolder.Name.Contains(oldvalue))
                {
                    System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(NextFolder.FullName);

                    folder.MoveTo(NextFolder.Parent.FullName + "\\" + Regex.Replace(NextFolder.Name, oldvalue, newvalue));
                }

            }

            Console.WriteLine($"完成替换");
        }


        /// <summary>
        /// 替换值
        /// </summary>
        /// <param name="strFilePath">txt等文件的路径</param>
        /// <param name="strIndex">索引的字符串，定位到某一行</param>
        /// <param name="newValue">替换新值</param>
        private static void ReplaceValue(string strFilePath, string oldValue, string newValue)
        {
            if (File.Exists(strFilePath))
            {
                string strContent = File.ReadAllText(strFilePath);
                strContent = Regex.Replace(strContent, oldValue, newValue);
                File.WriteAllText(strFilePath, strContent);
            }
        }
    }
}
